package com.googlecode.jch.plugin.configuration;

import com.intellij.openapi.ui.Messages;
import com.intellij.ui.AddEditDeleteListPanel;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

/**
 * @author Iskander Akishev
 */
class PackagesAddDeleteListPanel extends AddEditDeleteListPanel<String> {
	private final String myQuery;

	public PackagesAddDeleteListPanel(String title, String query) {
		super(title, Collections.<String>emptyList());
		myQuery = query;
	}

	void applyTo(List<String> packages) {
		packages.clear();
		for (Object o : getListItems()) {
			packages.add((String) o);
		}
	}

	void resetFrom(List<String> packages) {
		myListModel.clear();
		for (String p : packages) {
			myListModel.addElement(p);
		}
	}

	@Override
	@Nullable
	protected String findItemToAdd() {
		return showEditDialog("");
	}

	@Override
	protected String editSelectedItem(String item) {
		return showEditDialog(item);
	}

	@Nullable
	private String showEditDialog(final String initialValue) {
		return Messages.showInputDialog(this, myQuery, "Inlining package", Messages.getQuestionIcon(), initialValue, null);
	}
}
